package com.pureut.storage.service.impl;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.pureut.storage.domain.StockOutInfoDetail;
import com.pureut.storage.dto.NewStockInfoDetailDto;
import com.pureut.storage.mapper.StockOutInfoDetailMapper;
import com.pureut.storage.service.StockInfoDetailService;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.system.api.RemoteUnitService;
import com.sale.system.api.domain.SysAuxiliary;
import com.sale.system.api.domain.SysDictData;
import com.sale.system.api.model.MaterialUnitDto;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 领料出库明细实现层
 * @author: CM
 * @date: 2023/2/2  15:11
 */
@Service
public class StockInfoDetailServiceImpl extends ServiceImpl<StockOutInfoDetailMapper, StockOutInfoDetail> implements StockInfoDetailService {

    @Resource
    StockOutInfoDetailMapper stockOutInfoDetailMapper;

    /**
     * 单位Mapper
     */
    @DubboReference
    RemoteUnitService remoteUnitService;

    /**
     * 生产领料明细列表
     * @param docNum
     * @return
     */
    @Override
    public TableDataInfo<NewStockInfoDetailDto> getList(String docNum, PageQuery pageQuery) {
        //获取单据状态字典数据
        List<SysDictData> netArray = CacheUtils.get(CacheNames.SYS_DICT, "stock_out_info_detail_status");
        Map<String, String> statusMap = netArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        Page<NewStockInfoDetailDto> list = stockOutInfoDetailMapper.getStockInfoDetailList(pageQuery.build(),docNum);
        //单位集合
        Map<String, MaterialUnitDto> unitByCodeToMap = remoteUnitService.getUnitByCodeToMap();

        for (NewStockInfoDetailDto newStockInfoDetailDto : list.getRecords()) {

//            String unitName = remoteUnitService.getUnitDataByCode(newStockInfoDetailDto.getMaterialUnit()).getData().getUnitName();

            //辅助属性
            List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + newStockInfoDetailDto.getTenantId(), String.valueOf(newStockInfoDetailDto.getMaterialId()));
            newStockInfoDetailDto.setAuxiliaryAttribute(statsArray);

            newStockInfoDetailDto.setStatus(newStockInfoDetailDto.getStatus());
            newStockInfoDetailDto.setStatusDict(statusMap.get(newStockInfoDetailDto.getStatus()));

            newStockInfoDetailDto.setMaterialUnit(newStockInfoDetailDto.getMaterialUnit());
            if(unitByCodeToMap.get(String.valueOf(newStockInfoDetailDto.getMaterialUnit())) != null){
                newStockInfoDetailDto.setMaterialUnitDict(unitByCodeToMap.get(String.valueOf(newStockInfoDetailDto.getMaterialUnit())).getUnitName());
            }

        }
        return TableDataInfo.build(list);
    }
}
